package com.ll.menu.user.order;

import android.graphics.Paint;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.droid.library.app.App;
import com.droid.library.widget.recyclerview.BaseRecyclerAdapter;
import com.droid.library.widget.recyclerview.BaseViewHolder;
import com.ll.menu.user.R;
import com.ll.menu.user.app.entity.Food;
import com.ll.menu.user.dialog.StatusDialog;

/**
 * 已点Adapter
 *
 * @author wangheng
 */

public class OrderAdapter extends BaseRecyclerAdapter<Food> {


    public OrderAdapter(Fragment fragment){
        super(fragment);
    }

    @Override
    protected BaseViewHolder onCreateRealViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = mActivity.getLayoutInflater();
        View view = inflater.inflate(R.layout.item_order,parent,false);
        return new Holder(mFragment,view, OrderAdapter.this);
    }

    static class Holder extends BaseViewHolder<Food>{
        TextView nameTextView;
        TextView currentPriceTextView;
        TextView oldPriceTextView;
        TextView numberTextView;
        TextView completedTextView;
        TextView statusTextView;


        private String mCurrentPriceMode = null;
        private String mOldPriceMode = null;
        private String mCountMode = null;

        private int colorGray;
        private int colorOrange;


        public Holder(Fragment fragment, View itemView, OrderAdapter adapter) {
            super(itemView,fragment,adapter);

            colorGray = App.getInstance().getResources().getColor(R.color.color_gray);
            colorOrange = App.getInstance().getResources().getColor(R.color.color_orange);

            mCurrentPriceMode = App.getInstance().getString(R.string.cart_current_price_mode);
            mOldPriceMode = App.getInstance().getString(R.string.cart_old_price_mode);
            mCountMode = App.getInstance().getString(R.string.order_number_mode);

            nameTextView = (TextView) itemView.findViewById(R.id.tvOrderFoodName);
            currentPriceTextView = (TextView) itemView.findViewById(R.id.tvOrderCurrentPrice);
            oldPriceTextView = (TextView) itemView.findViewById(R.id.tvOrderOldPrice);
            numberTextView = (TextView) itemView.findViewById(R.id.tvOrderNumber);
            completedTextView = (TextView) itemView.findViewById(R.id.tvOrderComplete);
            statusTextView = (TextView) itemView.findViewById(R.id.tvOrderStatus);

        }

        @Override
        public void onBindViewHolder(int position, Food item) {

            // 名称
            nameTextView.setText(item.getName());

            // 现价
            currentPriceTextView.setText(String.format(mCurrentPriceMode,item.getCurrentPrice()));

            // 原价
            if(item.getOldPrice() != item.getCurrentPrice()) {
                oldPriceTextView.setVisibility(View.VISIBLE);
                oldPriceTextView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                oldPriceTextView.setText(String.format(mOldPriceMode, item.getOldPrice()));
            }else{
                oldPriceTextView.setVisibility(View.GONE);
            }

            // 数量
            numberTextView.setText(String.format(mCountMode,item.getCount()));

            // 是否已完成
            if(item.isCompleted()){
                completedTextView.setBackgroundColor(colorOrange);
                completedTextView.setText(R.string.order_completed);
            }else{
                completedTextView.setBackgroundColor(colorGray);
                completedTextView.setText(R.string.order_not_completed);
            }

            // 状态
            if(item.isReturn()){
                statusTextView.setTextColor(colorGray);
                statusTextView.setText(R.string.order_status_return);
                statusTextView.setOnClickListener(null);
            }else if(item.isSale()){
                statusTextView.setTextColor(colorGray);
                statusTextView.setText(R.string.order_status_sale);
                statusTextView.setOnClickListener(null);
            }else{
                statusTextView.setTextColor(colorOrange);
                statusTextView.setText(R.string.order_view_status);
                statusTextView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new StatusDialog(mActivity).show();
                    }
                });
            }

        }
    }
}
